package com.triz.business.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.triz.common.annotation.Log;
import com.triz.common.core.controller.BaseController;
import com.triz.common.core.domain.AjaxResult;
import com.triz.common.enums.BusinessType;
import com.triz.business.domain.ComponentInteraction;
import com.triz.business.service.IComponentInteractionService;
import com.triz.common.utils.poi.ExcelUtil;
import com.triz.common.core.page.TableDataInfo;

/**
 * 组件相互作用力关系Controller
 * 
 * @author hlk
 * @date 2025-11-14
 */
@RestController
@RequestMapping("/business/componentInteraction")
public class ComponentInteractionController extends BaseController
{
    @Autowired
    private IComponentInteractionService componentInteractionService;

    /**
     * 查询组件相互作用力关系列表
     */
//    @PreAuthorize("@ss.hasPermi('business:componentInteraction:list')")
    @GetMapping("/list")
    public TableDataInfo list(ComponentInteraction componentInteraction)
    {
        startPage();
        componentInteraction.setCreateBy(getUsername());
        List<ComponentInteraction> list = componentInteractionService.selectComponentInteractionList(componentInteraction);
        return getDataTable(list);
    }

    /**
     * 导出组件相互作用力关系列表
     */
//    @PreAuthorize("@ss.hasPermi('business:componentInteraction:export')")
    @Log(title = "组件相互作用力关系", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ComponentInteraction componentInteraction)
    {
        List<ComponentInteraction> list = componentInteractionService.selectComponentInteractionList(componentInteraction);
        ExcelUtil<ComponentInteraction> util = new ExcelUtil<ComponentInteraction>(ComponentInteraction.class);
        util.exportExcel(response, list, "组件相互作用力关系数据");
    }

    /**
     * 获取组件相互作用力关系详细信息
     */
//    @PreAuthorize("@ss.hasPermi('business:componentInteraction:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(componentInteractionService.selectComponentInteractionById(id));
    }

    /**
     * 新增组件相互作用力关系
     */
//    @PreAuthorize("@ss.hasPermi('business:componentInteraction:add')")
    @Log(title = "组件相互作用力关系", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ComponentInteraction componentInteraction)
    {
        return toAjax(componentInteractionService.insertComponentInteraction(componentInteraction));
    }

    /**
     * 修改组件相互作用力关系
     */
//    @PreAuthorize("@ss.hasPermi('business:componentInteraction:edit')")
    @Log(title = "组件相互作用力关系", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ComponentInteraction componentInteraction)
    {
        return toAjax(componentInteractionService.updateComponentInteraction(componentInteraction));
    }

    /**
     * 删除组件相互作用力关系
     */
//    @PreAuthorize("@ss.hasPermi('business:componentInteraction:remove')")
    @Log(title = "组件相互作用力关系", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(componentInteractionService.deleteComponentInteractionByIds(ids));
    }


    /**
     * 保存组件关系
     * @param list
     * @return
     */
    @Transactional
    @PostMapping("/saveInteractionRelations")
    public AjaxResult saveInteractionRelations(@RequestBody List<ComponentInteraction> list) {
        if (list.size()>0) {
            //1、先删除旧数据
            Map<String, Object> map = new HashMap<>();
            map.put("projectId", list.get(0).getProjectId());
            map.put("username", getUsername());
            componentInteractionService.deleteInteractionByProjectId(map);
        }

        //2、再添加新数据
        return toAjax(componentInteractionService.saveInteractionRelations(list));
    }


}
